package com.gues.ddc_app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gues.ddc_app.entity.ResultData;
import com.gues.ddc_app.entity.User;
import com.gues.ddc_app.service.impl.UserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huHang
 * @since 2023-02-12
 */
@RestController
@CrossOrigin//跨域
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("/queryAll")
    public ResultData<User> queryAll(Integer page, Integer limit,
                                     @RequestParam(defaultValue = "") String keyword){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.like("user_name",keyword);
        Page<User> userPage = new Page<>(page,limit);
        userService.page(userPage,wrapper);
        System.out.println(userPage);
        int a=(int)userPage.getTotal();
        ResultData<User> userResultData = new ResultData<>((Integer)a,userPage.getRecords());
        return userResultData;
    }
    @RequestMapping("/queryById{id}")
    public User queryById(@PathVariable(value = "id")Integer id){
        return userService.getById(id);
    }
    @RequestMapping("/update")
    public boolean update(String field,String value,Integer id){
        User user=new User();
        if(field.equals("address")){
            user.setAddress(value);
        }else if (field.equals("userPhone")){
            user.setUserPhone(value);
        }else {
            user.setUserPassword(value);
        }
        return userService.updateById(user);
    }
    @RequestMapping("/deleteByIds")
    public boolean deleteByIds(Integer[] ids){
        return userService.removeBatchByIds(Arrays.asList(ids));
    }
    @RequestMapping("addUser")
    public boolean addUser(User user){
        return userService.save(user);
    }
}
